package org.dromara.system.controller.miniyi;

import org.dromara.common.core.domain.R;
import org.dromara.common.core.enums.TabTypeMinYiEnum;
import org.dromara.common.core.utils.TimeUtil;
import org.dromara.system.domain.vo.ZServiceChartVO;
import org.dromara.system.domain.vo.ZServiceHeatMapVO;
import org.dromara.system.mapper.MapMapper;
import org.dromara.system.service.IZServiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 邹城民意热力图
 */
@RestController
@RequestMapping({"/heat"})
public class ZServiceHeatMapController {
    private static final Logger log = LoggerFactory.getLogger(ZServiceHeatMapController.class);
    @Autowired
    private MapMapper splashesMapper;
    @Autowired
    private IZServiceService izServiceService;//,派驻第七纪检监察组

    /**
     * 热力图
     *
     * @param timeCode  时间类型[1-当日,2-7天,3-30天,4-当前年份,5-全部,6-年月(6与开始时间，结束时间关联，其余1，2，3，4，5有参无影响)]
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param tabCode   模块名称 [1-高频热点,2-诉求类型,3-一级问题,4-二级问题,5-诉求渠道,6-三级问题]
     * @param chartName X轴参数
     * @return
     */
    @GetMapping({"map"})
    public R<List<ZServiceHeatMapVO>> heatMap(@RequestParam Integer timeCode,
                                              @RequestParam(defaultValue = "2022-01-01") String startTime,
                                              @RequestParam(defaultValue = "2022-01-15") String endTime,
                                              @RequestParam Integer tabCode,
                                              @RequestParam String chartName) {
        log.info("[ZServiceHeatMapController][heatMap]enter, timeCode:{}, startTime:{}, endTime:{}, tabCode:{}, chartName:{}", new Object[]{timeCode, startTime, endTime, tabCode, chartName});
        String time = TimeUtil.buildTime(timeCode, startTime, endTime);
        List<ZServiceHeatMapVO> heatMaps = this.splashesMapper.getHeatMap(chartName, time, TabTypeMinYiEnum.getTypeByCode(tabCode));
        return R.ok(heatMaps);
    }

    /**
     * 获取Top1高频事件的散点
     *
     * @param timeCode  时间类型[1-当日,2-7天,3-30天,4-当前年份,5-全部,6-年月(6与开始时间，结束时间关联，其余1，2，3，4，5有参无影响)]
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param tabCode   模块名称 [1-高频热点,2-诉求类型,3-一级问题,4-二级问题,5-诉求渠道,6-三级问题]
     * @param topNum    排序数值
     * @return
     */
    @GetMapping({"topProblemsMap"})
    public R<List<ZServiceHeatMapVO>> topProblemsMap(@RequestParam Integer timeCode,
                                                     @RequestParam(defaultValue = "2022-01-01") String startTime,
                                                     @RequestParam(defaultValue = "2022-01-15") String endTime,
                                                     @RequestParam Integer tabCode,
                                                     @RequestParam(defaultValue = "1") int topNum) {
        log.info("[ZServiceHeatMapController][topProblemsMap]enter, timeCode:{}, startTime:{}, endTime:{}", new Object[]{timeCode, startTime, endTime});
        String time = TimeUtil.buildTime(timeCode, startTime, endTime);
        List<ZServiceChartVO> hotProblemList = this.izServiceService.getHotProblem(time);
        log.info("[ZServiceHeatMapController][topProblemsMap]hotProblemList:{}, timeCode:{}, startTime:{}, endTime:{}", new Object[]{hotProblemList, timeCode, startTime, endTime});
        if (topNum < 1) {
            topNum = 1;
        }

        return hotProblemList != null && hotProblemList.size() >= topNum ? this.heatMap(timeCode, startTime, endTime, tabCode, ((ZServiceChartVO) hotProblemList.get(topNum - 1)).getChartName()) : R.ok((new ArrayList()));
    }
}
